using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.Reflection;
using System.IO;

namespace DiscGolfScore
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class PlayerForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ListView lsvPlayerNames;
		private System.Windows.Forms.ColumnHeader firstName;
		private System.Windows.Forms.ColumnHeader lastName;
		private System.Windows.Forms.Button btnStart;
		private System.Windows.Forms.ListBox lstCourses;
		private ArrayList players = new ArrayList();
		private System.Windows.Forms.MenuItem mitExit;
		private System.Windows.Forms.MenuItem mitTools;
		private System.Windows.Forms.Label lblPlayers;
		private System.Windows.Forms.Label lblCourse;
		private System.Windows.Forms.MenuItem miAbout;
		private HoleForm holeForm = new HoleForm();

		public PlayerForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			lstCourses.BackColor = System.Drawing.Color.LimeGreen;
			lsvPlayerNames.BackColor = System.Drawing.Color.LimeGreen;
			btnStart.BackColor = System.Drawing.Color.White;

			String currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);

			readPlayers(currentDirectory+@"\Players.xml");
			readCourses(currentDirectory+@"\Courses.xml");
			lstCourses.SelectedIndex = 0; //make sure a course is allways selected.
		}

		/// <summary>
		/// Read players xml file and store in players ArrayList.
		/// </summary>
		/// <param name="fileName"></param>
		private void readPlayers(string fileName) 
		{
			XmlTextReader xmlReader = new XmlTextReader(fileName);
			xmlReader.Read();	//read <players>
			players.Clear();
			lsvPlayerNames.Clear();
			while(xmlReader.Read()) 
			{	//read <player>
				if( xmlReader.Name.Equals("Player") )
				{
					Player p = new Player();
					p.FirstName = xmlReader.GetAttribute("FirstName");
					p.LastName = xmlReader.GetAttribute("LastName");
					p.Id = int.Parse(xmlReader.GetAttribute("id"));
					players.Add(p);
					lsvPlayerNames.Items.Add( new ListViewItem(new string[] {p.FirstName, p.LastName}) );
				}
			}
			lsvPlayerNames.Refresh();
		}
		/// <summary>
		/// Read courses xml file and store in list.
		/// </summary>
		/// <param name="fileName"></param>
		private void readCourses(string fileName) 
		{
			XmlTextReader xmlReader = new XmlTextReader(fileName);
			lstCourses.Items.Clear();
			while(xmlReader.Read()) 
			{	//read <Course>
				if( xmlReader.Name.Equals("Course") )
				{
					Course c = new Course();
					c.Name = xmlReader.GetAttribute("Name");
					c.Id = int.Parse(xmlReader.GetAttribute("id"));
					//read holes
					string holeXml = xmlReader.ReadInnerXml();
					XmlTextReader xmlHoleReader = new XmlTextReader(holeXml, XmlNodeType.Element, null);
					while(xmlHoleReader.Read()) 
					{	//read <Hole>
						if( xmlHoleReader.Name.Equals("Hole") )
						{
							Hole h = new Hole();
							h.par = int.Parse(xmlHoleReader.GetAttribute("Par"));
							h.description = xmlHoleReader.GetAttribute("Description");
							if( xmlHoleReader.GetAttribute("Length")!=null)
							{
								h.length = int.Parse(xmlHoleReader.GetAttribute("Length"));
							}
							c.holes.Add(h);
						}
					}
					
					lstCourses.Items.Add(c);
				}
			}
		}


		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}
		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PlayerForm));
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.mitTools = new System.Windows.Forms.MenuItem();
			this.mitExit = new System.Windows.Forms.MenuItem();
			this.lsvPlayerNames = new System.Windows.Forms.ListView();
			this.firstName = new System.Windows.Forms.ColumnHeader();
			this.lastName = new System.Windows.Forms.ColumnHeader();
			this.btnStart = new System.Windows.Forms.Button();
			this.lstCourses = new System.Windows.Forms.ListBox();
			this.lblPlayers = new System.Windows.Forms.Label();
			this.lblCourse = new System.Windows.Forms.Label();
			this.miAbout = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.Add(this.mitTools);
			// 
			// mitTools
			// 
			this.mitTools.MenuItems.Add(this.miAbout);
			this.mitTools.MenuItems.Add(this.mitExit);
			this.mitTools.Text = "Tools";
			// 
			// mitExit
			// 
			this.mitExit.Text = "Exit";
			this.mitExit.Click += new System.EventHandler(this.mitExit_Click);
			// 
			// lsvPlayerNames
			// 
			this.lsvPlayerNames.CheckBoxes = true;
			this.lsvPlayerNames.Columns.Add(this.firstName);
			this.lsvPlayerNames.Columns.Add(this.lastName);
			this.lsvPlayerNames.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
			this.lsvPlayerNames.Location = new System.Drawing.Point(8, 24);
			this.lsvPlayerNames.Size = new System.Drawing.Size(112, 212);
			this.lsvPlayerNames.View = System.Windows.Forms.View.SmallIcon;
			// 
			// firstName
			// 
			this.firstName.Text = "First Name";
			this.firstName.Width = 20;
			// 
			// lastName
			// 
			this.lastName.Text = "Last Name";
			this.lastName.Width = 20;
			// 
			// btnStart
			// 
			this.btnStart.Location = new System.Drawing.Point(8, 240);
			this.btnStart.Size = new System.Drawing.Size(224, 20);
			this.btnStart.Text = "Start new round";
			this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
			// 
			// lstCourses
			// 
			this.lstCourses.DisplayMember = "Name";
			this.lstCourses.Location = new System.Drawing.Point(128, 24);
			this.lstCourses.Size = new System.Drawing.Size(104, 223);
			// 
			// lblPlayers
			// 
			this.lblPlayers.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
			this.lblPlayers.Location = new System.Drawing.Point(8, 8);
			this.lblPlayers.Size = new System.Drawing.Size(104, 20);
			this.lblPlayers.Text = "Players:";
			// 
			// lblCourse
			// 
			this.lblCourse.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold);
			this.lblCourse.Location = new System.Drawing.Point(128, 8);
			this.lblCourse.Text = "Course:";
			// 
			// miAbout
			// 
			this.miAbout.Text = "About";
			this.miAbout.Click += new System.EventHandler(this.miAbout_Click);
			// 
			// PlayerForm
			// 
			this.BackColor = System.Drawing.Color.LawnGreen;
			this.Controls.Add(this.btnStart);
			this.Controls.Add(this.lstCourses);
			this.Controls.Add(this.lsvPlayerNames);
			this.Controls.Add(this.lblPlayers);
			this.Controls.Add(this.lblCourse);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu1;
			this.Text = "Disc Golf Score";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Application.Run(new PlayerForm());
		}

		private void btnStart_Click(object sender, System.EventArgs e)
		{
			//get all playing players
			ArrayList activePlayers = new ArrayList();
			for( int i=0; i< lsvPlayerNames.Items.Count; i++ )
			{
				if( lsvPlayerNames.Items[i].Checked ) 
				{
					activePlayers.Add( players[i] );
				}
			}

			//Max 6 players
			if( activePlayers.Count > 6 ) 
			{
				MessageBox.Show("Maximum 6 players allowed.", "Error");
				return;
			}

			//add hole data to players
			Course currentCourse = (Course)lstCourses.SelectedItem;
			if( activePlayers.Count > 0 ) 
			{
				foreach (Player p in activePlayers) 
				{
					p.holesPlaying.Clear();
					foreach( Hole hole in currentCourse.holes ) 
					{
						Hole playHole = hole.Copy();
						p.holesPlaying.Add(playHole);
					}
				}
				holeForm.Show(this, activePlayers, currentCourse);
				Hide();
			}
		}

		private void mitExit_Click(object sender, System.EventArgs e)
		{
			holeForm.Dispose();
			Dispose();
		}

		private void miAbout_Click(object sender, System.EventArgs e)
		{
			MessageBox.Show("DiscGolfScore by Lars Daniel Benes (c) 2006", "About");
		}

	}
}
